package com.bjfu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.eduservice.entity.EduTeacher;
import com.bjfu.eduservice.entity.vo.TeacherQuery;
import com.bjfu.eduservice.mapper.EduTeacherMapper;
import com.bjfu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-29
 */
@Service
public class EduEduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
        // 创建Wrapper对象
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        if(teacherQuery == null){
            return;
        }

        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //mybatis学过动态sql
        //判断条件值是否为空，如果不为空拼接
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        //可以使用Spring提供的StringUtils提供的函数来判空
        baseMapper.selectPage(pageParam,wrapper);
    }

    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageTeacher) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        // 分页数据封装到pageTeacher中
        baseMapper.selectPage(pageTeacher,wrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("current",pageTeacher.getCurrent());
        map.put("records",pageTeacher.getRecords());
        map.put("pages",pageTeacher.getPages());
        map.put("size",pageTeacher.getSize());
        map.put("total",pageTeacher.getTotal());
        map.put("next",pageTeacher.hasNext());
        map.put("previous",pageTeacher.hasPrevious());
        return map;
    }


}
